package com.geek.factory.controller;

import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.ProductStock;
import com.geek.factory.result.Result;
import com.geek.factory.service.ProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stock")
public class ProductStockController {


    @Autowired
    private ProductStockService productStockService;

    /**
     * 获取所有产品库存列表
     */
    @GetMapping("/list")
    public Result listAll() {
        List<ProductStock> stockList = productStockService.list();
        return new Result(SuccessConstant.SUCCESS, "查询成功", stockList);
    }

    /**
     * 根据产品ID查询库存
     */
    @GetMapping("/get/{productId}")
    public Result getByProductId(@PathVariable Integer productId) {
        ProductStock stock = productStockService.getByProductId(productId);
        return stock != null
                ? new Result(SuccessConstant.SUCCESS, "查询成功", stock)
                : new Result(SuccessConstant.FAIL, "未找到库存记录");
    }

    /**
     * 添加库存记录
     */
    @PostMapping("/add")
    public Result add(@RequestBody ProductStock stock) {
        boolean created = productStockService.save(stock);
        return created
                ? new Result(SuccessConstant.SUCCESS, "添加成功")
                : new Result(SuccessConstant.FAIL, "添加失败");
    }

    /**
     * 更新库存记录
     */
    @PutMapping("/update")
    public Result update(@RequestBody ProductStock stock) {
        boolean updated = productStockService.updateById(stock);
        return updated
                ? new Result(SuccessConstant.SUCCESS, "更新成功")
                : new Result(SuccessConstant.FAIL, "更新失败");
    }

    /**
     * 删除库存记录
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean removed = productStockService.removeById(id);
        return removed
                ? new Result(SuccessConstant.SUCCESS, "删除成功")
                : new Result(SuccessConstant.FAIL, "删除失败");
    }



}
